package com.foreknow.eldclient.service.impl;

import com.foreknow.eldclient.dao.BusinessMapper;
import com.foreknow.eldclient.dao.FoodMapper;
import com.foreknow.eldclient.dao.OrderDetailetMapper;
import com.foreknow.eldclient.dao.OrdersMapper;
import com.foreknow.eldclient.po.Business;
import com.foreknow.eldclient.po.Food;
import com.foreknow.eldclient.po.Orders;
import com.foreknow.eldclient.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BusinessServiceImpl implements BusinessService {
    @Autowired
    private BusinessMapper BM;
    @Autowired
    private FoodMapper FM;
    @Autowired
    private OrderDetailetMapper ODM;
    @Autowired
    private OrdersMapper OM;
    @Override
    @Transactional
    public Business getBusinessById(int businessId) {
        return BM.getBusinessById(businessId);
    }

    @Override
    public List<Business> listBusinessByOrderTypeId(int OrderTypeId) {
        List<Business> list = BM.listBusinessByOrderTypeId(OrderTypeId);
        return list;
    }

    @Override
    public Integer registerBusiness(Business business) {
        BM.registerBusiness(business);
        return business.getBusinessId();
    }

    @Override
    public Business getBusinessByIdByPass(String businessId,String password) {
        return BM.getBusinessByIdByPass(businessId,password);
    }

    @Override
    public List<Business> listBusinessBykeyword(String keyword) throws Exception {
        List<Business> list = BM.listBusinessBykeyword(keyword);
        for (Business business : list) {
            business.setFoodList(FM.listFoodBykeyword(keyword,business.getBusinessId()));
        }
        return list;
    }

    @Override
    public List<Business> listAllBusiness() {
        List<Business> list = BM.listAllBusiness();
        return list;
    }

    @Override
    public int updateBusiness(Business business) {
        return BM.updateBusiness(business);
    }

    @Override
    public int updateTotalSales(Integer orderId) throws Exception {
        Orders order = OM.getOrdersById(orderId);
        Business business = order.getBusiness();
        int sales = OM.getTotalSales(business.getBusinessId());
        business.setTotalSales(sales++);
        return BM.updateTotalSales(business);
    }

    @Override
    public List<Business> sales_ranking() {
        return BM.sales_ranking();
    }

    @Override
    public Business getBusinessByFId(int foodId) {
        return BM.getBusinessByFId(foodId);
    }

    @Override
    public List<Business> getBusinessByFuzzy(String keyword) throws Exception {
        List<Business> list = BM.getBusinessByFuzzy(keyword);
        for (Business business : list) {
            Integer businessId = business.getBusinessId();
            List<Food> foods = FM.listFoodBykeyword(keyword, businessId);
            business.setFoodList(foods);

        }
        return list;
    }
}
